package com.example.administrator.caigou51.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.SearchActivity;
import com.example.administrator.caigou51.bean.CategoryBean;
import com.example.administrator.caigou51.bean.MainDataBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.recyclerCard.card.SortLeftCard;
import com.example.administrator.caigou51.recyclerCard.card.SortRightCard;
import com.example.administrator.caigou51.request.BaseResponse;
import com.example.administrator.caigou51.request.ICallBackForRequestServer;
import com.example.administrator.caigou51.request.RequestServerManager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2015/8/11.
 */
public class SortFragment extends BaseFragment  implements View.OnClickListener{
    private View holderView;
    private boolean isScaled=false;
    private MaterialListView materialListViewLeft;
    private MaterialListView materialListViewRight;
    private RelativeLayout relativeLayout_top;
    private CategoryBean categoryBean ;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      //  EventBus.getDefault().register(this);
        if (holderView == null) {
            holderView = inflater.inflate(R.layout.fragment_sort, container, false);
            materialListViewLeft= (MaterialListView) holderView.findViewById(R.id.materialListViewLeft);
            materialListViewRight= (MaterialListView) holderView.findViewById(R.id.materialListViewRight);
            relativeLayout_top= (RelativeLayout) holderView.findViewById(R.id.relativeLayout_top);relativeLayout_top.setOnClickListener(this);
            initView();
            TaskGetMainData(false);
        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }
        return holderView;
    }

    private void initView() {


        materialListViewRight.setColumnLayout(2);

    }

    private void fillArrayInListView(CategoryBean categoryBean) {
        materialListViewLeft.clear();
        materialListViewRight.clear();
        for (int i=0;i<categoryBean.getFirstcat().size();i++)
        {
            SortLeftCard sortLeftCard=new SortLeftCard(getActivity());
            sortLeftCard.setFirstcatEntity(categoryBean.getFirstcat().get(i));
            sortLeftCard.setMaterialListViewRight(materialListViewRight);
            sortLeftCard.setCategoryBean(categoryBean);
            if (i==0) {
                sortLeftCard.setSelectEd(true);
            }else
            {
                sortLeftCard.setSelectEd(false);
            }
            sortLeftCard.setMaterialListView(materialListViewLeft);
            materialListViewLeft.add(sortLeftCard);
        }

        for (int i=0;i<categoryBean.getSecondcat().size();i++)
        {
            if (categoryBean.getFirstcat().get(0).getCatid().equals(categoryBean.getSecondcat().get(i).getParentid()))
            {
                CategoryBean.SecondcatEntity entity=new CategoryBean.SecondcatEntity();
                entity.setCatid(categoryBean.getSecondcat().get(i).getCatid());
                entity.setCatname(categoryBean.getSecondcat().get(i).getCatname());
                entity.setParentid(categoryBean.getSecondcat().get(i).getParentid());

                SortRightCard sortRightCard = new SortRightCard(getActivity());
                sortRightCard.setSecondcatEntity(entity);
                materialListViewRight.add(sortRightCard);
            }
        }


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.relativeLayout_top:
                getActivity().startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    private void TaskGetMainData(final boolean isloadmore) {
        RequestServerManager.getInstanceDIY().handleMethodDIY((GBaseActivity) getActivity(), materialListViewLeft, isloadmore, null,
                Constant.getRootUrl() + Constant.Action.Action_Category, Constant.Action.Action_Category, RequestServerManager.getInstanceDIY().
                        getRequestParams(Constant.Action.Action_Category), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        // JSONObject jsonObject = (JSONObject) result.getData();
                        categoryBean = JSON.parseObject(result.getData().toString(), CategoryBean.class);
                        fillArrayInListView(categoryBean);

                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {

                    }
                });
    }
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (categoryBean==null)
        {
            TaskGetMainData(false);
        }

    }
}


